package edu.pitt.cs.cs2410pj2.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
 * Read the traces
 * @author zhangfan
 *
 */
public class TraceReader {
	public static ArrayList<Trace> readTraces(String traceFile) throws IOException {
		ArrayList<Trace> traces = new ArrayList<Trace>();
		BufferedReader reader = new BufferedReader(new FileReader(traceFile));
		String line = reader.readLine();
		while(line!=null) {
			String[] tokens = line.split("\\s");
			if(tokens.length>3) {
				Trace trace = new Trace();
				trace.setCycle(Integer.parseInt(tokens[0]));
				trace.setCordId(Integer.parseInt(tokens[1]));
				if(tokens[2].equals("0")) {
					trace.setTypeRead(true);
				} else {
					trace.setTypeRead(false);
				}
				trace.setAddr(tokens[3]);
				traces.add(trace);
			}
			line = reader.readLine();
		}
		return traces;
	}
	
	public static void main(String[] args) throws IOException {
		ArrayList<Trace> traces = readTraces("traces.txt");
		for(int i = 0;i<traces.size();i++) {
			System.out.println(traces.get(i));
		}
	}
}
